//
//  NeirongViewController.m
//  App-MyFairldy
//
//  Created by 客官大大 on 15/5/27.
//  Copyright (c) 2015年 客官大大. All rights reserved.
//

#import "NeirongViewController.h"
#import "ShoucahngTool.h"

@interface NeirongViewController ()

@end

@implementation NeirongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self.navigationController setNavigationBarHidden:YES];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-shoucang1"] style:UIBarButtonItemStylePlain target:self action:@selector(touchiright)];

    activitivw = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activitivw.color = [UIColor redColor];
    activitivw.center = self.view.center;
    [self.view addSubview:activitivw];
    [activitivw startAnimating];
    
    
    [self download];
    
    
}
-(void)touchih
{

}
-(void)download
{
    NSString *str = @"http://food.boohee.com/fb/v1/foods/caomei";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
         model = [[Jiexi alloc] init];
        NSError *error1 = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error1];

        self.dataarry = [NSMutableArray array];
        model.appraise = [dic objectForKey:@"appraise"];
        model.calory = [dic objectForKey:@"calory"];
        model.carbohydrate = [dic objectForKey:@"carbohydrate"];
        model.code = [dic objectForKey:@"code"];
        model.large_image_url = [dic objectForKey:@"large_image_url"];
        model.thumb_image_url = [dic objectForKey:@"thumb_image_url"];
        model.name = [dic objectForKey:@"name"];
        
        NSDictionary *dicc = [dic objectForKey:@"ingredient"];
    


            model.fat = [dicc objectForKey:@"fat"];
            model.fiber_dietary = [dicc objectForKey:@"fiber_dietary"];
            model.protein = [dicc objectForKey:@"protein"];
            model.carotene = [dicc objectForKey:@"carotene"];
            model.iron = [dicc objectForKey:@"iron"];
            model.kalium = [dicc objectForKey:@"kalium"];
            model.magnesium = [dicc objectForKey:@"magnesium"];
            model.manganese = [dicc objectForKey:@"manganese"];
            model.natrium = [dicc objectForKey:@"natrium"];
            model.vitamin_a = [dicc objectForKey:@"vitamin_a"];
            model.vitamin_c = [dicc objectForKey:@"vitamin_c"];
            model.vitamin_e = [dicc objectForKey:@"vitamin_e"];
            
   
        
       NSDictionary *dica = [dic objectForKey:@"lights"];

            model.caloryiii = [dica objectForKey:@"calory"];
            model.carbohydrateiii = [dica objectForKey:@"carbohydrate"];
            model.fatiii = [dica objectForKey:@"fat"];
            model.fiber_dietaryiii = [dica objectForKey:@"fiber_dietary"];
            model.proteiniii = [dica objectForKey:@"protein"];

       
        

            [self.dataarry addObject:model];
            
        
        [self inittableview];
    }];

}
-(void)touchiright
{

    [ShoucahngTool openDatabse];
    [ShoucahngTool createTable];
    NSLog(@"%@",NSHomeDirectory());
    NSArray *arr = [ShoucahngTool selectData];
    if (arr.count == 0) {
        ShoucangModel *model1 = [[ShoucangModel alloc] init];
        model1.name = model.name;
        model1.appraise = model.appraise;
        model1.calory = model.calory;
        [ShoucahngTool insertValue:model1];
        alt = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alt show];
        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(shoucang) userInfo:nil repeats:NO];
        
    }else{
        NSLog(@"%@",arr[0]);
        for (ShoucangModel *model2 in arr) {
            NSLog(@"%@",model2.name);
            if ([model2.name isEqualToString:model.name]) {
                alt = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"已收藏该食物" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alt show];
                [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(shoucang) userInfo:nil repeats:NO];
             
             
           
            }else if ([model2.name isEqualToString:[[arr lastObject]name]]){
                ShoucangModel *model1 = [[ShoucangModel alloc] init];
                model1.name = model.name;
                model1.appraise = model.appraise;
                model1.calory = model.calory;
                [ShoucahngTool insertValue:model1];
                alt = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alt show];
                [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(shoucang) userInfo:nil repeats:NO];
            
            }else{
                alt = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"收藏失败" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alt show];
                [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(shoucang) userInfo:nil repeats:NO];
            
            }
                
        }
    }
    
    
    
    
//    [ShoucahngTool openDatabse];
//    [ShoucahngTool createTable];
//    ShoucangModel *model1 = [[ShoucangModel alloc] init];
//    model1.name = model.name;
//    model1.pingjia = model.appraise;
//    model1.kaluli = model.calory;
//    NSLog(@"%@",model1.name);
//    [ShoucahngTool insertValue:model1];
//    alt = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
//    [alt show];
//    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(shoucang) userInfo:nil repeats:NO];
    
    
}
-(void)shoucang
{
  [alt dismissWithClickedButtonIndex:0 animated:NO];
}
-(void)inittableview
{
    UIScrollView *scro = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 667)];
    scro.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];

    [self.view addSubview:scro];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 80)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *lable7 = [[UILabel alloc] initWithFrame:CGRectMake(100, 5, 200, 40)];
    lable7.text = model.name;
    [view addSubview:lable7];
    UILabel *lable4 = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 200, 20)];
    lable4.text = model.calory;
    lable4.textColor = [UIColor redColor];
    [view addSubview:lable4];
    
    NSURL *url = [NSURL URLWithString:model.thumb_image_url];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image3 = [UIImage imageWithData:data];
    UIImageView *iji = [[UIImageView alloc] initWithFrame:CGRectMake(15, 5, 70, 70)];
    iji.image = image3;
    [view addSubview:iji];
    
    UILabel *lablelll = [[UILabel alloc] initWithFrame:CGRectMake(140, 50, 200, 20)];
    lablelll.text = @"卡路里/100g(可使用部分)";
    
    [view addSubview:lablelll];
    
    [scro addSubview:view];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0,130, 375, 150)];
    view1.backgroundColor = [UIColor whiteColor];
    UIImage *iiii = [UIImage imageNamed:@"iconfont-cantingjieshao2"];
    UIImageView *vvvv = [[UIImageView alloc] initWithFrame:CGRectMake(15, 30, 80, 80)];
    vvvv.image = iiii;
    [view1 addSubview:vvvv];
    UILabel *lil = [[UILabel alloc] initWithFrame:CGRectMake(35, 110, 40, 20)];
    lil.text = @"推荐";
    [view1 addSubview:lil];
    
    UILabel *lal = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 270, 140)];
    lal.text = model.appraise;
    lal.numberOfLines = 0;
    [lal setLineBreakMode:NSLineBreakByWordWrapping];
    [view1 addSubview:lal];
    
    
    
    
    
    [scro addSubview:view1];
    
    scro.contentSize = CGSizeMake(375, 667);
    scro.pagingEnabled = NO;
    scro.bounces = NO;
    [scro setContentOffset:CGPointMake(0, 0)];
    
    scro.delegate = self;
    
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 330, 375, 280) style:UITableViewStylePlain];
    
    table.delegate = self;
    table.dataSource = self;
    [scro addSubview:table];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 100, 20)];
    lable.text = @"健康评价";
    [lable setFont:[UIFont systemFontOfSize:20]];
    
    [scro addSubview:lable];
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 310, 375, 20)];
    lable1.text = @"营养元素";
    [scro addSubview:lable1];
    UILabel *lable2 = [[UILabel alloc]initWithFrame:CGRectMake(190, 310, 375, 20)];
    lable2.text = @"每100克";
    [scro addSubview:lable2];
    UILabel *lable3 = [[UILabel alloc]initWithFrame:CGRectMake(310, 310, 375, 20)];
    lable3.text = @"备注";
    [scro addSubview:lable3];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *indent = @"cell";
    NeirongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indent];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"NeirongTableViewCell" owner:self options:nil]lastObject];
    }
    if (indexPath.row == 0) {
        cell.lable1.text = @"热量";
        cell.lable2.text = model.calory;
        cell.lable3.text = model.caloryiii;
    }
    if (indexPath.row == 1) {
        cell.lable1.text = @"蛋白质";
        cell.lable2.text = model.protein;
        cell.lable3.text = model.proteiniii;
    }
    if (indexPath.row == 2) {
        cell.lable1.text = @"脂肪";
        cell.lable2.text = model.fat;
        cell.lable3.text = model.fatiii;
    }
    if (indexPath.row == 3) {
        cell.lable1.text = @"碳水化合物";
        cell.lable2.text = model.carbohydrate;
        cell.lable3.text = model.carbohydrateiii;
    }
    if (indexPath.row == 4) {
        cell.lable1.text = @"膳食纤维";
        cell.lable2.text = model.fiber_dietary;
        cell.lable3.text = model.fiber_dietaryiii;
    }
    if (indexPath.row == 5) {
        cell.lable1.text = @"维生素A";
        cell.lable2.text = model.vitamin_a;
       
    }
    if (indexPath.row == 6) {
        cell.lable1.text = @"维生素C";
        cell.lable2.text = model.vitamin_c;
 
    }
    if (indexPath.row == 7) {
        cell.lable1.text = @"维生素E";
        cell.lable2.text = model.vitamin_e;
    }
    if (indexPath.row == 8) {
        cell.lable1.text = @"胡萝卜素";
        cell.lable2.text = model.carotene;

    }
    if (indexPath.row == 9) {
        cell.lable1.text = @"铁";
        cell.lable2.text = model.iron;
}
    if (indexPath.row == 10) {
        cell.lable1.text = @"钾";
        cell.lable2.text = model.kalium;

    }
    if (indexPath.row == 11) {
        cell.lable1.text = @"镁";
        cell.lable2.text = model.magnesium;
   
    }
    if (indexPath.row == 12) {
        cell.lable1.text = @"锰";
        cell.lable2.text = model.manganese;
        
    }
    if (indexPath.row == 13) {
        cell.lable1.text = @"钠";
        cell.lable2.text = model.natrium;
        
    }

    
    
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 14;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
